package com.itstudio.myforumsystem.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itstudio.myforumsystem.common.Constants;
import com.itstudio.myforumsystem.common.Result;
import com.itstudio.myforumsystem.entity.User;
import com.itstudio.myforumsystem.entity.dto.UserDTO;
import com.itstudio.myforumsystem.mapper.UserMapper;
import com.itstudio.myforumsystem.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@ResponseBody
@RequestMapping("/user")
@Slf4j
public class UserController  {
    @Autowired
    private UserService userService;

    /**
     * 新增用户
     * @param
     * @return
     */
    @PostMapping
    public Result save(@RequestBody User user){
        log.info("新增员工：{}",user);
        userService.saveOrUpdate(user);

        //TODO 修改文章
        return Result.success();
    }


    @GetMapping
    public List<User> findAll(){
        return userService.list();//查找所有数据
    }

    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Integer id){
        return  userService.removeById(id);
    }

    //批量删除
    @PostMapping("/del/batch")
    public boolean deleteBatch(@RequestBody List<Integer> ids){
        return userService.removeByIds(ids);
    }

    //用户登录
    @PostMapping("/login")
    public Result login(@RequestBody UserDTO userDTO){
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();

        if(StrUtil.isBlank(username)||StrUtil.isBlank(password)){
            return Result.error(Constants.CODE_400,"参数错误！");
        }

        UserDTO dto = userService.login(userDTO);

        return Result.success(dto);
    }

    //    管理员登录
    @PostMapping("/adminLogin")
    public Result AdminLogin(@RequestBody UserDTO userDTO){
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        String role = userDTO.getRole();

        if(StrUtil.isBlank(username)||StrUtil.isBlank(password)||StrUtil.isBlank(role)){
            return Result.error(Constants.CODE_400,"参数错误！");
        }
        UserDTO dto = userService.adminLogin(userDTO);
        System.out.println(dto.getToken());
        return Result.success(dto);
    }


    //注册
    @PostMapping("/register")
    public Result register(@RequestBody UserDTO userDTO){

        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        Integer count=userService.register(username);
        if (count>=1){
            return Result.error(Constants.CODE_400,"用户名已存在！");
        }else{
            User user=new User();
            user.setUsername(username);
            user.setPassword(password);
            userService.save(user);
            return Result.success();
        }
    }
    //通过username拿到数据
    @GetMapping("/username/{username}")
    public Result personDate(@PathVariable String username){
        User user = userService.UserDataFindByUsername(username);
        if(user!=null){
            return Result.success(user);
        }else{
            return  Result.error(Constants.CODE_400,"参数错误！");
        }
    }

    //通过id拿到数据

    @GetMapping("/findUserInfor/{articleId}")
    public Result findUserByArticle_user(@PathVariable Integer articleId){

        User user = userService.findUserByArticle_user(articleId);
        if(user!=null){
            return Result.success(user);
        }else{
            return  Result.error(Constants.CODE_400,"参数错误！");
        }
    }
    @GetMapping("/PersonalCenter/{userId}")
    public Result findPersonalInforById(@PathVariable Integer userId)
    {
        return Result.success( userService.allInforOfPersonal(userId));
    }
    @GetMapping("/recommandUser")
    public  Result recommandUser(){
        return Result.success(userService.fiveUser());
    }
    //查询用户被浏览次数
    @GetMapping("/findReadNumber/{id}")
    public Result findReadNumber(@PathVariable Integer id)
    {
        return Result.success(userService.findReadNumber(id));
    }
    //查询用户被收藏量
    @GetMapping("/findCollectionNumber/{id}")
    public  Result findCollectiongNumber(@PathVariable Integer id)
    {
        return Result.success(userService.findCollectionNumber(id));
    }
    //分页查询  --- mybatisplus
    @GetMapping("/page")
    public IPage<User> findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam(defaultValue = "") String username,//避免搜索为空时的报错
                                @RequestParam(defaultValue = "") String email,
                                @RequestParam(defaultValue = "") String address){
        IPage<User> page = new Page<User>(pageNum,pageSize);
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        if(!"".equals(username)){
            queryWrapper.like("username",username);//框架是直接在中间加了一个and的
        }
        if(!"".equals(address)){
            // queryWrapper.or().like("nickname",nickname);  添加or 但是添加了or的话 会使其它的条件无效 也不是 这样说 就是或者的意思
            queryWrapper.like("address",address);
        }
        if(!"".equals(email)){
            queryWrapper.like("email",email);//框架是直接在中间加了一个and的
        }
        return userService.page(page,queryWrapper);

    }


}

